import { getExtenion } from '../../../../../utils/util'
Component({
  properties: {
    detail: {
      type: Object
    }
  },
  data: {
    info: {}
  },
  options: {
    styleIsolation: 'shared'
  },
  observers: {
    detail: function(detail) {
      if (detail.files==='') {
        detail.fileList = []
      } else if (detail.files.indexOf(',') > 0) {
        detail.fileList = detail.files.split(',')
      } else {
        detail.fileList = [detail.files]
      }
      this.setData({
        info: detail
      })
    }
  },
  methods: {
    toDetail({ currentTarget = {} }) {
      const _this = this
      const fileUrl = currentTarget.dataset.url
      const ext = getExtenion(fileUrl)
      if (!_this.isImage(ext) && !_this.isVideo(ext)) {
        if (['pdf', 'doc', 'docx', 'ppt', 'pptx'].indexOf(ext) !== -1) {
          wx.downloadFile({
            // 示例 url，并非真实存在
            url: fileUrl,
            success: function (res) {
              const filePath = res.tempFilePath
              wx.openDocument({ 
                filePath: filePath,
                success() {
                  console.log('打开成功')
                }
              })
            }
          })
        }
      }
    },
    isImage(ext) {
      return ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff'].indexOf(ext) !== -1;
    },
    isVideo(ext) {
      return ['mp4', 'mov', 'm4v', '3gp', 'avi', 'm3u8', 'webm'].indexOf(ext) !== -1;
    },
    playVideo(e) {
      //执行全屏方法  
      var videoContext = wx.createVideoContext('video', this);
      videoContext.requestFullScreen();
      this.setData({ fullScreen:true })
   },
   /**关闭视屏 */
   closeVideo() {
     //执行退出全屏方法
     var videoContext = wx.createVideoContext('video', this);
     videoContext.exitFullScreen();     
   },
  }
})
